package package02;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @Description TODO
 * @Author zzj
 * @Date 2020/7/16 13:26
 */
public class DBUtil {
    String driverClassName = "oracle.jdbc.OracleDriver";
    String url = "jdbc:oracle:thin:@39.105.173.35:1521:helowin";
    String username = "scott";
    String password = "123456";
    Connection conn = null;

    /**
     * @param
     * @Description 连接数据库
     * @Return java.sql.Connection
     * @Author zzj
     * @Date 2020/7/16 13:34
     **/
    public Connection init() {
        // 加载驱动
        try {
            Class.forName(driverClassName);
            // 创建会话
            conn = DriverManager.getConnection(url, username, password);
            // 数据库连接成功
            System.out.println("数据库连接成功");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * @param
     * @Description 数据库关闭
     * @Return void
     * @Author zzj
     * @Date 2020/7/16 13:37
     **/
    public void close() {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }


}
